package com.example.monitordemo.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import lombok.experimental.UtilityClass;

import java.util.Date;

import static com.example.monitordemo.config.MonitorConfig.*;
import static com.example.monitordemo.constant.CommonConstant.*;
import static com.example.monitordemo.constant.LinuxConstant.*;


/**
 * linux监控工具类
 */
@UtilityClass
public class LinuxMonitorUtil {

    /**
     * 查询语句
     */
    String promQL;


    /**
     * 运行时间
     * @return
     */
    public JSON runTime(Date time,String instance){
        return getJson(time,instance,RUN_TIME);
    }
    /**
     * 总内存
     * @return
     */
    public JSON cpuNum(Date time,String instance){
        return getJson(time,instance,CPU_NUM);
    }
    /**
     * CPU iowait
     * @return
     */
    public JSON cpuIowait(Date time,String instance){
        return getJson(time,instance,CPU_IOWAIT);
    }
    /**
     * 总CPU使用率
     * @return
     */
    public JSON cpuUseRate(Date time,String instance){
        return getJson(time,instance,CPU_USE_RATE);
    }
    /**
     * 内存使用率
     * @return
     */
    public JSON memoryUseRate(Date time,String instance){
        return getJson(time,instance,MEMORY_USE_RATE);
    }
    /**
     * 交换分区使用率
     * @return
     */
    public JSON swap(Date time,String instance){
        return getJson(time,instance,SWAP);
    }
    /**
     * 各分区总空间(EXT.*&#x2F;XFS)
     * @return
     */
    public JSON eachDiskTotalSpace(Date time,String instance){
        return getJson(time,instance,EACH_DISK_TOTAL_SPACE);
    }

    /**
     * 各分区可用空间(EXT.*&#x2F;XFS)
     * @return
     */
    public JSON eachDiskFreeSpace(Date time,String instance){
        return getJson(time,instance,EACH_DISK_FREE_SPACE);
    }
    /**
     * 每小时流量下载ens33
     * @return
     */
    public JSON hourNetReceived(Date time,String instance){
        return getJson(time,instance,HOUR_NET_RECEIVED);
    }
    /**
     * 每小时流量上传ens33
     * @return
     */
    public JSON hourNetSent(Date time,String instance){
        return getJson(time,instance,HOUR_NET_SENT);
    }
    /**
     * CPU系统使用率
     * @return
     */
    public JSON cpuSystemUseRate(Date time,String instance){
        return getJson(time,instance,CPU_SYSTEM_USE_RATE);
    }
    /**
     * CPU用户使用率
     * @return
     */
    public JSON cpuUserUseRate(Date time,String instance){
        return getJson(time,instance,CPU_USER_USE_RATE);
    }
    /**
     * CPU磁盘使用率
     * @return
     */
    public JSON cpuDiskUseRate(Date time,String instance){
        return getJson(time,instance,CPU_DISK_USE_RATE);
    }
    /**
     * CPU总使用率
     * @return
     */
    public JSON cpuSumUseRate(Date time,String instance){
        return getJson(time,instance,CPU_SUM_USE_RATE);
    }
    /**
     * 单个实例总内存
     * @return
     */
    public JSON instanceSumMemory(Date time,String instance){
        return getJson(time,instance,INSTANCE_SUM_MEMORY);
    }
    /**
     * 已用内存
     * @return
     */
    public JSON usedMemory(Date time,String instance){
        return getJson(time,instance,USED_MEMORY);
    }
    /**
     * 剩余内存
     * @return
     */
    public JSON instanceFreeMemory(Date time,String instance){
        return getJson(time,instance,INSTANCE_FREE_MEMORY);
    }
    /**
     * 实例已用内存
     * @return
     */
    public JSON instanceUsedMemory(Date time,String instance){
        return getJson(time,instance,INSTANCE_USED_MEMORY);
    }
    /**
     * 每秒宽带接收ens33
     * @return
     */
    public JSON secondNetReceived(Date time,String instance){
        return getJson(time,instance,SECOND_NET_RECEIVED);
    }
    /**
     * 每秒宽带发送ens33
     * @return
     */
    public JSON secondNetSent(Date time,String instance){
        return getJson(time,instance,SECOND_NET_SENT);
    }
    /**
     * 系统1分钟负载
     * @return
     */
    public JSON system1MinuteLoad(Date time,String instance){
        return getJson(time,instance,SYSTEM_1MINUTE_LOAD);
    }
    /**
     * 系统5分钟负载
     * @return
     */
    public JSON system5MinutesLoad(Date time,String instance){
        return getJson(time,instance,SYSTEM_5MINUTES_LOAD);
    }
    /**
     * 系统15分钟负载
     * @return
     */
    public JSON system15MinutesLoad(Date time,String instance){
        return getJson(time,instance,SYSTEM_15MINUTES_LOAD);
    }
    /**
     * 系统CPU总核数
     * @return
     */
    public JSON systemCpuNum(Date time,String instance){
        return getJson(time,instance,SYSTEM_CPU_NUM);

    }
    /**
     * 磁盘每秒读
     * @return
     */
    public JSON diskSecondRead(Date time,String instance){
        return getJson(time,instance,DISK_SECOND_READ);
    }
    /**
     * 磁盘每秒写
     * @return
     */
    public JSON diskSecondWrite(Date time,String instance){
        return getJson(time,instance,DISK_SECOND_WRITE);
    }
    /**
     * 磁盘使用率
     * @return
     */
    public JSON diskUseRate(Date time,String instance){
        return getJson(time,instance,DISK_USE_RATE);
    }
    /**
     * 磁盘读速率
     * @return
     */
    public JSON diskReadSpeed(Date time,String instance){
        return getJson(time,instance,DISK_READ_SPEED);
    }
    /**
     * 磁盘写速率
     * @return
     */
    public JSON diskWriteSpeed(Date time,String instance){
        return getJson(time,instance,DISK_WRITE_SPEED);
    }
    /**
     * 每1秒内I&#x2F;O操作耗时占比
     * @return
     */
    public JSON secondIoPercentage(Date time,String instance){
        return getJson(time,instance,SECOND_IO_PERCENTAGE);
    }
    /**
     * 每次IO读的耗时（参考：小于100ms）(beta)
     * @return
     */
    public JSON secondIoReadTime(Date time,String instance){
        return getJson(time,instance,SECOND_IO_READ_TIME);
    }
    /**
     * 每次IO写耗时（参考：小于100ms）(beta)
     * @return
     */
    public JSON secondIoWriteTime(Date time,String instance){
        return getJson(time,instance,SECOND_IO_WRITE_TIME);
    }
    /**
     * Tcp_CurrEstab
     * @return
     */
    public JSON netTcpCurrEstab(Date time,String instance){
        return getJson(time,instance,NET_TCP_CURR_ESTAB);
    }
    /**
     * sockstat_TCP_tw
     * @return
     */
    public JSON socketTcpTw(Date time,String instance){
        return getJson(time,instance,SOCKET_TCP_TW);
    }
    /**
     * sockstat_sockets_used
     * @return
     */
    public JSON socketUsed(Date time,String instance){
        return getJson(time,instance,SOCKET_USED);
    }
    /**
     * SOCKET_INUSE
     * @return
     */
    public JSON socketInuse(Date time,String instance){
        return getJson(time,instance,SOCKET_INUSE);
    }
    /**
     * SOCKET_TCP_ALLOC
     * @return
     */
    public JSON socketTcpAlloc(Date time,String instance){
        return getJson(time,instance,SOCKET_TCP_ALLOC);
    }
    /**
     * NET_TCP_IN_SEGS
     * @return
     */
    public JSON netTcpInSegs(Date time,String instance){
        return getJson(time,instance,NET_TCP_IN_SEGS);
    }
    /**
     * NET_TCP_OUT_SEGS
     * @return
     */
    public JSON netTcpOutSegs(Date time,String instance){
        return getJson(time,instance,NET_TCP_OUT_SEGS);
    }
    /**
     * NET_TCP_RETRANS_SEGS
     * @return
     */
    public JSON netTcpRetransSegs(Date time,String instance){
        return getJson(time,instance,NET_TCP_RETRANS_SEGS);
    }
    /**
     * 使用的文件描述符
     * @return
     */
    public JSON fileAllocated(Date time,String instance){
        return getJson(time,instance,FILE_ALLOCATED);
    }
    /**
     * 每秒上下文切换次数
     * @return
     */
    public JSON contextSwitches(Date time,String instance){
        return getJson(time,instance,CONTEXT_SWITCHES);
    }


    /**
     * 判断是否替换application和instance
     * @param time 时间
     * @param instance linux_exporter IP和端口
     * @param query 原promQL语句
     * @return
     */
    public static JSON getJson(Date time, String instance, String query) {
        String t = PromQLUtil.parseDateToTime(time);
        promQL = query;
        if (StrUtil.isNotBlank(instance)){
            promQL = promQL.replace(LINUX_INSTANCE,instance);
        }
        String executePromQl = API_PREFIX + INSTANT_QUERY_PREFIX + "query=" + promQL + "&time="+t;
        return PromQLUtil.executePromQl(executePromQl);
    }

}
